﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ChangeMode : MonoBehaviour
{
    public GameObject mode1;
    public GameObject mode2;
    public GameObject mode3;
    public Transform cubeRoot;
    public ControlRotate slider1;
    public ControlRotate slider2;
    public ControlRotate slider3;

    private const float minDis = 0.1f;
    private List<List<List<Transform>>> cubeLst = new List<List<List<Transform>>>();
    private List<Transform> surfaceLst = new List<Transform>();
    private List<Cube> cubeDataLst = new List<Cube>();

    private int curMode = 1;

    // Use this for initialization
    void Start()
    {
        for (int i = 0; i < 9; i++)
        {
            string name = string.Format("surface{0}", i + 1);
            surfaceLst.Add(cubeRoot.Find(name));
        }

        for (int i = 0; i < cubeRoot.childCount; i++)
        {
            Transform child = cubeRoot.GetChild(i);
            if (child.name.StartsWith("Cube"))
            {
                Cube cube = new Cube();
                Vector3 pos = child.position;
                cube.Index = pos;
                cube.Pos = pos;
                cube.tran = child;
                cubeDataLst.Add(cube);
                child.name = string.Format("Cube{0}-{1}-{2}", (int)pos.x, (int)pos.y, (int)pos.z);
            }
        }

        // for (int i = 0; i < 3; i++)
        // {
        //     List<List<Transform>> cubeInSurface = new List<List<Transform>>();
        //     for (int j = 0; j < 3; j++)
        //     {
        //         List<Transform> cubeInLine = new List<Transform>();
        //         for (int k = 0; k < 3; k++)
        //         {
        //             string name = string.Format("Cube{0}-{1}-{2}", i + 1, j + 1, k + 1);
        //             Transform cubeTran = cubeRoot.Find(name);
        //             cubeInLine.Add(cubeTran);

        //             Cube cube = new Cube();
        //             cube.Index = new Vector3(i, j, k);
        //             cube.Pos = cubeTran.position + Vector3.one;
        //             cubeDataLst.Add(cube);
        //         }
        //         cubeInSurface.Add(cubeInLine);
        //     }
        //     cubeLst.Add(cubeInSurface);
        // }

        Refresh();
    }

    private void FreshSliderControl()
    {
        slider1.curMode = curMode;
        slider2.curMode = curMode;
        slider3.curMode = curMode;

        if (curMode == 1)
        {
            slider1.surfaceRotate = surfaceLst[0].GetComponent<AutoRotate>();
            slider2.surfaceRotate = surfaceLst[1].GetComponent<AutoRotate>();
            slider3.surfaceRotate = surfaceLst[2].GetComponent<AutoRotate>();
        }
        else if (curMode == 2)
        {
            slider1.surfaceRotate = surfaceLst[3].GetComponent<AutoRotate>();
            slider2.surfaceRotate = surfaceLst[4].GetComponent<AutoRotate>();
            slider3.surfaceRotate = surfaceLst[5].GetComponent<AutoRotate>();
        }
        else
        {
            slider1.surfaceRotate = surfaceLst[6].GetComponent<AutoRotate>();
            slider2.surfaceRotate = surfaceLst[7].GetComponent<AutoRotate>();
            slider3.surfaceRotate = surfaceLst[8].GetComponent<AutoRotate>();
        }
    }

    private void FreshModeUI()
    {
        mode1.SetActive(curMode == 1);
        mode2.SetActive(curMode == 2);
        mode3.SetActive(curMode == 3);
    }

    private void ApplyReParent()
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    if (curMode == 1)
                    {
                        Cube cube = cubeDataLst.Find((data) =>
                        {
                            return Vector3.Distance(data.Pos, new Vector3(i, j, k)) < minDis;
                        });

                        Transform cubeTran = cube.tran;
                        cubeTran.SetParent(surfaceLst[k]);
                    }
                    else if (curMode == 2)
                    {
                        Cube cube = cubeDataLst.Find((data) =>
                        {
                            return Vector3.Distance(data.Pos, new Vector3(k, i, j)) < minDis;
                        });
                        Transform cubeTran = cube.tran;
                        cubeTran.SetParent(surfaceLst[k + 3]);
                    }
                    else
                    {
                        Cube cube = cubeDataLst.Find((data) =>
                        {
                            return Vector3.Distance(data.Pos, new Vector3(i, k, j)) < minDis;
                        });
                        Transform cubeTran = cube.tran;
                        cubeTran.SetParent(surfaceLst[k + 6]);
                    }
                }
            }
        }

        // if (curMode == 1)
        // {
        //     for (int i = 0; i < 3; i++)
        //     {
        //         for (int j = 0; j < 3; j++)
        //         {
        //             for (int k = 0; k < 3; k++)
        //             {
        //                 Transform cube = cubeLst[k][i][j];
        //                 cube.SetParent(surfaceLst[k]);
        //             }
        //         }
        //     }
        // }
        // else if (curMode == 2)
        // {
        //     for (int i = 0; i < 3; i++)
        //     {
        //         for (int j = 0; j < 3; j++)
        //         {
        //             for (int k = 0; k < 3; k++)
        //             {
        //                 Transform cube = cubeLst[i][j][k];
        //                 cube.SetParent(surfaceLst[k + 3]);
        //             }
        //         }
        //     }
        // }
        // else
        // {
        //     for (int i = 0; i < 3; i++)
        //     {
        //         for (int j = 0; j < 3; j++)
        //         {
        //             for (int k = 0; k < 3; k++)
        //             {
        //                 Transform cube = cubeLst[i][k][j];
        //                 cube.SetParent(surfaceLst[k + 6]);
        //             }
        //         }
        //     }
        // }
    }

    public void ChangeModeLogic()
    {
        curMode++;
        if (curMode > 3)
        {
            curMode = 1;
        }

        Refresh();
    }

    public void Refresh()
    {
        FreshModeUI();
        ApplyReParent();
        FreshSliderControl();
    }

    public void ReCalcCubePos()
    {
        for (int i = 0; i < cubeDataLst.Count; i++)
        {
            Cube cube = cubeDataLst[i];
            cube.Pos = cube.tran.position;
            Vector3 pos = cube.Pos;
            cube.tran.name = string.Format("Cube{0}-{1}-{2}", (int)pos.x, (int)pos.y, (int)pos.z);
        }

        ApplyReParent();
    }
}
